package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.persistence.AccountDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AccountService {
    @Autowired
    private AccountDAO accountDAO;

    public Account getAccount(String username){
        return accountDAO.getAccountByUsername(username);


    }

    public Account getAccount(String username,String password){
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        return accountDAO.getAccountByUsernameAndPassword(account);
    }

    /*声明式事务处理*/
    @Transactional
    public void insertAccount(Account account){
        accountDAO.insertAccount(account);
        accountDAO.insertProfile(account);
        accountDAO.insertSignOn(account);
    }

    public void updateAccount(Account account){
        accountDAO.updateAccount(account);
        accountDAO.updateProfile(account);

        if(account.getPassword() != null && account.getPassword().length() > 0){
            accountDAO.updateSignOn(account);
        }
    }

    //获取所有用户的列表
    public List<Account> getAccountList(){
        return accountDAO.getAccountList();
    }
}
